//
//  FeedbackViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/12/1.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FeedbackViewController.h"
#import "SKPSMTPMessage.h"
#import "MBProgressHUD.h"
#import "RESideMenu.h"

#define SCREEN_WIDTH    ([[UIScreen mainScreen] bounds].size.width / 375)
#define SCREEN_HEIGHT   ([[UIScreen mainScreen] bounds].size.height / 667)

@interface FeedbackViewController ()<SKPSMTPMessageDelegate>

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) SKPSMTPMessage *myMessage;


@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    
    UIImageView *backImg = [[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:backImg];
    [backImg setImage:[UIImage imageNamed:@"5.png"]];
    
    //毛玻璃效果
    UIBlurEffect *effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView=[[UIVisualEffectView alloc]initWithEffect:effect];
    effectView.frame= self.view.frame;
    //    effectView.alpha = 0.8;
    [backImg addSubview:effectView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-category"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendEmail)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    
    [self initTextView];
}

- (void)initTextView
{
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20 * SCREEN_WIDTH, 20 * SCREEN_HEIGHT, WIDTH - 40 * SCREEN_WIDTH, 200 * SCREEN_HEIGHT)];
    [self.view addSubview:self.textView];
    self.textView.backgroundColor = [UIColor whiteColor];
    self.textView.font = [UIFont systemFontOfSize:17];
    self.textView.text = @"欢迎您提出宝贵意见!";
    self.textView.keyboardType = UIKeyboardTypeDefault;

}

#pragma mark - 发送邮件方法
- (void)sendEmail
{
    
    self.myMessage = [[SKPSMTPMessage alloc] init];
    self.myMessage.fromEmail = @"rwzrwz449@163.com";
    self.myMessage.toEmail = @"18940033425@163.com";
    self.myMessage.relayHost = @"smtp.163.com";
    self.myMessage.requiresAuth = YES;
    if (self.myMessage.requiresAuth) {
        self.myMessage.login = @"rwzrwz449@163.com";
        self.myMessage.pass = @"rwzrwz449@";
    }
    self.myMessage.wantsSecure = YES;
    self.myMessage.subject = @"用户反馈信息";
    self.myMessage.delegate = self;
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                         self.textView.text,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    self.myMessage.parts = [NSArray arrayWithObjects:dic, nil];
    [self.myMessage send];

}

#pragma mark - SKPSMTPMessageDelegate必须实现方法
- (void)messageSent:(SKPSMTPMessage *)message
{
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.mode = MBProgressHUDModeText;
    self.hud.minShowTime = 2;
    [self.hud showAnimated:YES whileExecutingBlock:^{
        
        self.hud.labelText = @"发送成功";
        
    } completionBlock:^{
        
        [self.hud removeFromSuperview];
        self.hud = nil;
        
    }];
    
    
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    NSLog(@"失败");
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.mode = MBProgressHUDModeText;
    [self.hud showAnimated:YES whileExecutingBlock:^{
        
        self.hud.labelText = @"发送失败";
        
    } completionBlock:^{
        
        [self.hud removeFromSuperview];
        //        [_hud release];
        self.hud = nil;
        
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.myMessage.delegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
